class MilestonesController < ApplicationController
before_filter :check_authorization
  layout "project"
  # GET /milestones
  # GET /milestones.xml
  def index
    @act = params[:act]
    @milestones = Milestone.find(:all)
    if @project.milestones.size > 0
      @data = gantt_data(@project)[:data]
      @height = gantt_data(@project)[:height]
    end
    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @milestones.to_xml }
    end
  end

  # GET /milestones/1
  # GET /milestones/1.xml
  def show
    @milestone = Milestone.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @milestone.to_xml }
    end
  end

  # GET /milestones/new
  def new
    @milestone = Milestone.new
  end

  # GET /milestones/1;edit
  def edit
    @milestone = Milestone.find(params[:id])
  end

  # POST /milestones
  # POST /milestones.xml
  def create
    @milestone = Milestone.new(params[:milestone])
    respond_to do |format|
      if @milestone.save
        flash[:notice] = 'Milestone was successfully created.'
	if @milestone.mail_alert
	  Notifier.deliver_notify('new_milestone',@milestone.user.email,@milestone,@milestone.user.lang) if @milestone.user.email.match("@")
	end
        format.html { redirect_to milestones_url(@milestone.project) }
        format.xml  { head :created, :location => milestone_url(@milestone) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @milestone.errors.to_xml }
      end
    end
  end

  # PUT /milestones/1
  # PUT /milestones/1.xml
  def update
    @milestone = Milestone.find(params[:id])

    respond_to do |format|
      if @milestone.update_attributes(params[:milestone])
        flash[:notice] = 'Milestone was successfully updated.'
        format.html { redirect_to milestones_url(@milestone.project) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @milestone.errors.to_xml }
      end
    end
  end

  # DELETE /milestones/1
  # DELETE /milestones/1.xml
  def destroy
    @milestone = Milestone.find(params[:id])
    @milestone.destroy

    respond_to do |format|
      format.html { redirect_to milestones_url }
      format.xml  { head :ok }
    end
  end

  def finish
    @ms = Milestone.find(params[:id])
    @ms.finished_at = Time.now
    @ms.save
    @project.reload
    @data = gantt_data(@project)[:data]
    render :update do |page|
     page<< 'updateChart(' + @data + ')'
     page.replace_html 'late_ms',:partial => 'late_milestones'
     page.replace_html 'upcoming_ms',:partial => 'upcoming_milestones'
     page.replace_html 'finished_ms',:partial => 'finished_milestones'
    end
  end
  def unfinish
    @ms = Milestone.find(params[:id])
    @ms.finished_at = 0
    @ms.save
    render :update do |page|
     page.replace_html 'late_ms',:partial => 'late_milestones'
     page.replace_html 'upcoming_ms',:partial => 'upcoming_milestones'
     page.replace_html 'finished_ms',:partial => 'finished_milestones'
    end
  end
end
